package com.charm.restaurant.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.charm.restaurant.common.Result;
import com.charm.restaurant.entity.Purchase;
import com.charm.restaurant.service.PurchaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 采购控制器
 */
@Slf4j
@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    /**
     * 新增采购单
     */
    @PostMapping
    public Result<Purchase> save(@RequestBody Purchase purchase) {
        log.info("新增采购单：{}", purchase);
        return purchaseService.savePurchase(purchase);
    }

    /**
     * 分页查询采购单
     */
    @GetMapping("/page")
    public Result<Page<Purchase>> page(int page, int pageSize, String name) {
        log.info("分页查询采购单，page={}, pageSize={}, name={}", page, pageSize, name);
        return purchaseService.pagePurchase(page, pageSize, name);
    }

    /**
     * 根据ID查询采购单
     */
    @GetMapping("/{id}")
    public Result<Purchase> getById(@PathVariable Long id) {
        log.info("根据ID查询采购单：{}", id);
        return purchaseService.getPurchaseById(id);
    }

    /**
     * 修改采购单
     */
    @PutMapping
    public Result<Purchase> update(@RequestBody Purchase purchase) {
        log.info("修改采购单：{}", purchase);
        return purchaseService.updatePurchase(purchase);
    }

    /**
     * 删除采购单
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids) {
        log.info("删除采购单：{}", ids);
        return purchaseService.deletePurchase(ids);
    }

    /**
     * 更新采购单状态
     */
    @PutMapping("/status")
    public Result<String> updateStatus(Long id, Integer status) {
        log.info("更新采购单状态，id={}, status={}", id, status);
        return purchaseService.updateStatus(id, status);
    }

    /**
     * 入库处理
     */
    @PutMapping("/instock/{id}")
    public Result<String> inStock(@PathVariable Long id) {
        log.info("采购单入库，id={}", id);
        return purchaseService.inStock(id);
    }
} 